/*
 * @(#)DefaultSwatchChooserPanel.java	1.34 05/11/17
 *
 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
 * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

package be.dch.gui.components;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;

import javax.swing.JPanel;

import org.apache.log4j.Logger;

import be.dch.util.ArrayUtils;

public class JRasterPanel extends JPanel {

    private static final Logger LOG = Logger.getLogger(JRasterPanel.class);

    private static final int RASTER_GAP = 1;
    private static final int RASTER_TILE_SIZE = 10;

    private static final long serialVersionUID = -6898069898319428991L;
    private transient Color[] colors;
    private final transient Dimension rasterSize = new Dimension(JRasterPanel.RASTER_TILE_SIZE, JRasterPanel.RASTER_TILE_SIZE);
    private final transient Dimension numPixels;
    private final transient Dimension gap = new Dimension(JRasterPanel.RASTER_GAP, JRasterPanel.RASTER_GAP);

    public JRasterPanel(final int numPixels) {
	super();
	this.numPixels = new Dimension(numPixels, numPixels);
	this.initValue();
	this.setToolTipText("");
	this.setOpaque(true);
	// setBackground(Color.white);
	this.setRequestFocusEnabled(false);
	this.setInheritsPopupMenu(true);
    }

    public JRasterPanel(final int numPixelsHeight, final int numPixelsWidth) {
	super();
	this.numPixels = new Dimension(numPixelsWidth, numPixelsHeight);
	this.initValue();
	this.setToolTipText("");
	this.setOpaque(true);
	// setBackground(Color.white);
	this.setRequestFocusEnabled(false);
	this.setInheritsPopupMenu(true);

    }

    private void initValue() {
	final int dimensions = (int) (this.numPixels.getHeight() * this.numPixels.getWidth());
	this.colors = new Color[] {};
	for (int i = 0; i < dimensions; i++) {
	    this.colors = (Color[]) ArrayUtils.add(this.colors, Color.white);
	}
    }

    @Override
    public boolean isFocusTraversable() {
	return false;
    }

    @Override
    public void paintComponent(final Graphics grap) {
	grap.setColor(this.getBackground());
	grap.fillRect(0, 0, this.getWidth(), this.getHeight());
	for (int row = 0; row < this.numPixels.height; row++) {
	    final int y = row * (this.rasterSize.height + this.gap.height);
	    for (int column = 0; column < this.numPixels.width; column++) {

		grap.setColor(this.getColorForCell(column, row));
		final int x = column * (this.rasterSize.width + this.gap.width);

		grap.fillRect(x, y, this.rasterSize.width, this.rasterSize.height);
		grap.setColor(Color.black);
		grap.drawLine((x + this.rasterSize.width) - 1, y, (x + this.rasterSize.width) - 1, (y + this.rasterSize.height) - 1);
		grap.drawLine(x, (y + this.rasterSize.height) - 1, (x + this.rasterSize.width) - 1, (y + this.rasterSize.height) - 1);
	    }
	}

    }

    @Override
    public Dimension getPreferredSize() {
	final int x = (this.numPixels.width * (this.rasterSize.width + this.gap.width)) - 1;
	final int y = (this.numPixels.height * (this.rasterSize.height + this.gap.height)) - 1;
	return new Dimension(x, y);
    }

    @Override
    public Dimension getSize() {
	return this.getPreferredSize();
    }

    @Override
    public Dimension getMaximumSize() {
	return this.getPreferredSize();
    }

    @Override
    public Dimension getMinimumSize() {
	return this.getPreferredSize();
    }

    @Override
    public String getToolTipText(final MouseEvent evt) {
	final Color color = this.getColorForLocation(evt.getX(), evt.getY());
	return color.getRed() + ", " + color.getGreen() + ", " + color.getBlue();
    }

    public Color getColorForLocation(final int x, final int y) {
	final int column = x / (this.rasterSize.width + this.gap.width);
	final int row = y / (this.rasterSize.height + this.gap.height);
	return this.getColorForCell(column, row);
    }

    private Color getColorForCell(final int column, final int row) {
	final int pixel = (row * this.numPixels.width) + column;
	return this.colors[pixel];

    }

    public void setColorForLocation(final int x, final int y, final Color color) {
	final int column = x / (this.rasterSize.width + this.gap.width);
	final int row = y / (this.rasterSize.height + this.gap.height);
	this.setColorForCell(column, row, color);
    }

    private void setColorForCell(final int column, final int row, final Color color) {
	final int pixel = (row * this.numPixels.width) + column;
	JRasterPanel.LOG.debug("Modifing pixel:" + pixel);
	this.colors[pixel] = color;
    }

    public BufferedImage getPixels() {
	final BufferedImage pixelImage = new BufferedImage(this.numPixels.width, this.numPixels.height, BufferedImage.TYPE_INT_RGB);
	for (int i = 0; i < this.colors.length; i++) {
	    final int x = i % this.numPixels.width;
	    final int y = i / this.numPixels.width;
	    pixelImage.setRGB(x, y, this.colors[i].getRGB());
	}
	return pixelImage;
    }

}
